package com.xsky.logic.platfault;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import com.xsky.common.util.UUIDUtil;
import com.xsky.common.util.XMLConverter;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.TabPlatFault;

public class PlatFaultLogic {

	BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();

	// 增加
	public String add(String xmlStr) {
		TabPlatFault entity = XMLConverter.convertToObject(xmlStr, TabPlatFault.class);
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setId(UUIDUtil.getUUID());
		}
		entity.setTimestamp(new Date());
		baseDao.insertEntity(entity);
		return "<data><result>ok</result><uuid>" + entity.getId() + "</uuid><info>添加成功</info></data>";
	}

	// 删除
	public String delete(String id) {
		TabPlatFault entity = queryById0(id);
		baseDao.delete(entity);
		return "<data><result>ok</result><uuid></uuid><info>删除成功</info></data>";
	}

	public String queryById(String id) {
		TabPlatFault entity = queryById0(id);
		if (entity == null) {
			return null;
		}
		return XMLConverter.convertToXML(entity);
	}

	private TabPlatFault queryById0(String id) {
		List list = baseDao.findByKey(new TabPlatFault(), "id", id);
		if (list == null || list.isEmpty()) {
			return null;
		}
		TabPlatFault entity = (TabPlatFault) list.get(0);
		return entity;
	}

	// 更新报送信息
	public String update(String xmlStr) {
		TabPlatFault entity = XMLConverter.convertToObject(xmlStr, TabPlatFault.class);
		entity.setTimestamp(new Date());
		baseDao.updateEntity(entity);
		return "<data><result>ok</result><uuid></uuid><info>更新成功</info></data>";
	}

	public String queryAll() {
		List list = baseDao.findAll(TabPlatFault.class.getName());
		return XMLConverter.convertToXML(list);
	}

	public String queryByCondition(String xml) {
		// <data>
		// <type>01</type>
		// <status>0</status> ps : 0表示未解决，1表示解决
		// <operatorId>0001</operatorId> 工程师id
		// <createTime_from></createTime_from>
		// <createTime_to></createTime_to>
		// <occurTime_from></occurTime_from>
		// <occurTime_to></occurTime_to>
		// <pageNum>4</pageNum><numPerPage>4</numPerPage>
		// </data>

		try {
			Document document = DocumentHelper.parseText(xml);
			String type = document.selectSingleNode("//data//type").getText().trim();
			String status = document.selectSingleNode("//data//status").getText().trim();
			String operatorId = document.selectSingleNode("//data//operatorId").getText().trim();
			String fromCreateTimeStr = document.selectSingleNode("//data//createTime_from").getText().trim();
			String toCreateTimeStr = document.selectSingleNode("//data//createTime_to").getText().trim();
			String fromOccuTimeStr = document.selectSingleNode("//data//occurTime_from").getText().trim();
			String toOccuTimeStr = document.selectSingleNode("//data//occurTime_to").getText().trim();

			int pageNum = Integer.parseInt(document.selectSingleNode("//data//pageNum").getText().trim());
			int numPerPage = Integer.parseInt(document.selectSingleNode("//data//numPerPage").getText().trim());

			if (pageNum < 2) {
				pageNum = 1;
			}

			String start = (1 + ((pageNum - 1) * numPerPage)) + "";

			StringBuilder sb = new StringBuilder(" 1=1 ");
			if (StringUtils.isNotEmpty(type)) {
				sb.append(" and type ='" + type + "'");
			}

			if (StringUtils.isNotEmpty(status)) {
				if ("0".equals(status)) {
					sb.append(" and resolveTime > '0'");
				} else if ("1".equals(status)) {
					sb.append(" and resolveTime is null");
				}
			}

			if (StringUtils.isNotEmpty(operatorId)) {
				sb.append(" and operatorId ='" + operatorId + "'");
			}

			if (StringUtils.isNotEmpty(fromCreateTimeStr)) {
				sb.append(" and timestamp >= '" + fromCreateTimeStr + "'");
			}
			if (StringUtils.isNotEmpty(toCreateTimeStr)) {
				sb.append(" and timestamp <= '" + toCreateTimeStr + "'");
			}
			if (StringUtils.isNotEmpty(fromOccuTimeStr)) {
				sb.append(" and occurTime >= '" + fromOccuTimeStr + "'");
			}
			if (StringUtils.isNotEmpty(toCreateTimeStr)) {
				sb.append(" and occurTime <= '" + toOccuTimeStr + "'");
			}

			// 这接口用起来快死人了....
			List list = baseDao.fetchByPagesWithCondition(TabPlatFault.class.getName(), start, numPerPage + "", sb
					.toString(), "timestamp", "asc");
			return XMLConverter.convertToXML(list);

		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}

	private int getCount(int year) {

		String curYear = year + "-00-00";
		String nextYear = (year + 1) + "-00-00";

		String hql = "  timestamp > '" + curYear + "'";
		hql += " and timestamp < '" + nextYear + "'";
		return baseDao.fetchCount(TabPlatFault.class.getName(), hql);
	}

	private int getCount(int year, int month) {
		String monthStr = month > 9 ? month + "" : "0" + month;
		int nextMonth = month + 1;
		String nextMonthStr = nextMonth > 9 ? nextMonth + "" : "0" + nextMonth;
		String fromTime = year + "-" + monthStr + "-00";
		String toTime = year + "-" + nextMonthStr + "-00";

		String hql = "  timestamp > '" + fromTime + "'";
		hql += " and timestamp < '" + toTime + "'";
		return baseDao.fetchCount(TabPlatFault.class.getName(), hql);
	}

	public String  getCountOfCurYear() {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		return getCount(year)+"";
	}

	public String getCountOfCurMonth() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);

		return getCount(year, month)+"";
	}
}
